package com.dstz.bpm.engine.plugin.runtime.abstact;

import cn.hutool.core.collection.CollectionUtil;
import com.dstz.bpm.api.constant.ExtractType;
import com.dstz.bpm.api.engine.plugin.def.BpmUserCalcPluginDef;
import com.dstz.bpm.engine.model.BpmIdentity;
import com.dstz.bpm.engine.plugin.plugindef.AbstractUserCalcPluginDef;
import com.dstz.bpm.engine.plugin.runtime.BpmUserCalcPlugin;
import com.dstz.bpm.engine.plugin.session.BpmUserCalcPluginSession;
import com.dstz.org.api.model.IUser;
import com.dstz.org.api.service.UserService;
import com.dstz.sys.api.model.SysIdentity;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Resource;

public abstract class AbstractUserCalcPlugin<M extends BpmUserCalcPluginDef> implements BpmUserCalcPlugin<M> {
   @Resource
   protected UserService cb;

   protected abstract List<SysIdentity> queryByPluginDef(BpmUserCalcPluginSession var1, M var2);

   @Override
   public List<SysIdentity> execute(BpmUserCalcPluginSession pluginSession, M pluginDef) {
      if (pluginSession.isPreViewModel() && !this.supportPreView()) {
         return Collections.emptyList();
      } else {
         List<SysIdentity> list = this.queryByPluginDef(pluginSession, pluginDef);
         if (CollectionUtil.isEmpty(list)) {
            return list;
         } else {
            ExtractType extractType = ((AbstractUserCalcPluginDef)pluginDef).getExtract();
            Set<SysIdentity> set = new LinkedHashSet();
            List<SysIdentity> rtnList = new ArrayList();
            list = this.extract(list, extractType);
            set.addAll(list);
            rtnList.addAll(set);
            return rtnList;
         }
      }
   }

   protected List<SysIdentity> extract(List<SysIdentity> bpmIdentities, ExtractType extractType) {
      if (CollectionUtil.isEmpty(bpmIdentities)) {
         return Collections.EMPTY_LIST;
      } else {
         return extractType == ExtractType.EXACT_NOEXACT ? bpmIdentities : this.extractBpmIdentity(bpmIdentities);
      }
   }

   protected List<SysIdentity> extractBpmIdentity(List<SysIdentity> bpmIdentities) {
      List<SysIdentity> results = new ArrayList();
      Iterator var3 = bpmIdentities.iterator();

      while(true) {
         while(var3.hasNext()) {
            SysIdentity bpmIdentity = (SysIdentity)var3.next();
            if ("user".equals(bpmIdentity.getType())) {
               results.add(bpmIdentity);
            } else {
               List<? extends IUser> users = this.cb.getUserListByGroup(bpmIdentity.getType(), bpmIdentity.getId());
               Iterator var6 = users.iterator();

               while(var6.hasNext()) {
                  IUser user = (IUser)var6.next();
                  results.add(new BpmIdentity(user));
               }
            }
         }

         return results;
      }
   }

   public boolean supportPreView() {
      return true;
   }
}
